from .DBManager import DBManager

_test_data = {
    "department": {
        "cols": ('id', 'name', 'parent'),
        "values": [(1, '学院', 0),
                   (2, '1队', 1),
                   (3, '2队', 1),
                   (4, '1队1班', 2),
                   (5, '1队2班', 2),
                   (6, '2队1班', 3),
                   (7, '2队2班', 3),
                   ]
    },
    "user": {
        "cols": ('number', 'password', 'online', 'role', 'dep'),
        "values": [('1000', '1000', 0, 'admin', 1),
                   ('1001', '1001', 0, 'user', 4),
                   ('1002', '1002', 0, 'user', 6),
                   ]
    },
}


def db_data_import(data, dbfile):
    """
    # 导入数据data数据格式 说明
    data = {
        "User":  # key为表名
            {
                "cols":  # 指明要插入的列名
                    ('number', 'password', 'online', 'role'),
                "values":  # 行列表，列表每一项都是包含行内容的元组
                    [
                        ('1000', '1000', 0, 'admin'),
                        ('1001', '1001', 0, 'user'),
                        ('1002', '1002', 0, 'user'),
                    ]
            },
    }
    """
    if type(data) is not dict:
        print("data必须是dict类型！")
        return False
    else:
        db = DBManager()
        db.connect(dbfile)
        try:
            for tname, vals in data.items():
                if type(vals) is not dict:
                    raise TypeError('vals 必须是dic类型！')
                cols = vals.get("cols", None)
                if cols:
                    if isinstance(cols, str):
                        if ',' not in cols:
                            cols = tuple([cols])
                        else:
                            cols = tuple(cols.split(','))
                    if isinstance(cols, list):
                        cols = tuple(cols)
                    if not isinstance(cols, tuple):
                        raise TypeError('cols 必须是list,tuple类型！')
                    rows = vals.get("values", None)
                    if rows:
                        _rows = []
                        if len(cols) == 1:
                            for r in rows:
                                if isinstance(r, str):
                                    _rows.append([r])
                                else:
                                    _rows.append(r)
                        else:
                            _rows = rows
                        if isinstance(_rows, list):
                            _rows = tuple(_rows)
                        if not isinstance(_rows, tuple):
                            raise TypeError('rows 必须是list,tuple类型！')
                        db.insert_batch(tname, cols, _rows)
                    else:
                        return True
                else:
                    return True
        except TypeError as e:
            print("data结构不符合规范！")
            print("相关信息：", e)
            return False
        except Exception as e:
            print("数据导入失败！")
            print("相关信息：", e)
            return False
        finally:
            db.close()
